package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Signup;
import cn.edu.neusoft.yuejian4.yuejian_4.listener.TListener;
import cn.edu.neusoft.yuejian4.yuejian_4.model.ActivityModel;

/**
 * Created by Lix on 2018/10/18.
 */

public class ActivitySignUpActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView act_name,act_differ,realname,phone,act_unitprice,act_price,people_num;
    private Button signup;
    private ImageView back,jia,jian;
    private SharedPreferences sp;
    private Context mContext;
    private int unitprice,aid,uid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_signup);
        initViews();
        initEvent();
        sp = getSharedPreferences("userinfo",MODE_PRIVATE);
        aid = getIntent().getIntExtra("act_id",1);
        uid = sp.getInt("uid",4);
        ActivityModel activityModel = new ActivityModel();
        activityModel.signup(aid,uid,signuplis);
    }

    TListener<Signup> signuplis = new TListener<Signup>() {
        @Override
        public void onResponse(Signup signup) {
            act_name.setText(signup.getActivity_name());
            act_differ.setText(signup.getAct_type());
            realname.setText(signup.getReal_name());
            phone.setText(Integer.toString(signup.getPhone()));
            act_unitprice.setText(Integer.toString(signup.getActivity_price()));
            unitprice = signup.getActivity_price();
            act_price.setText(Integer.toString(signup.getActivity_price()));
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void initViews(){

        act_name = (TextView)findViewById(R.id.act_sign_name);
        act_differ=(TextView)findViewById(R.id.act_type);
        realname = (TextView)findViewById(R.id.act_realname);
        act_price = (TextView)findViewById(R.id.act_zprice);
        phone = (TextView)findViewById(R.id.act_sign_phone);
        act_unitprice = (TextView)findViewById(R.id.act_unitprice);
        people_num = (TextView)findViewById(R.id.act_peonum) ;
        signup = (Button)findViewById(R.id.act_signup);
        back = (ImageView)findViewById(R.id.act_sign_back);
        jia = (ImageView)findViewById(R.id.act_jia);
        jian = (ImageView)findViewById(R.id.act_jian);

    }
    private void initEvent(){
        signup.setOnClickListener(this);
        back.setOnClickListener(this);
        jia.setOnClickListener(this);
        jian.setOnClickListener(this);

    }

    public String toAllPrice(int num){

        int price = unitprice*num;
        String Price = Integer.toString(price);
        return Price;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.act_sign_back:
                finish();
                break;
            case R.id.act_jia:
                if(Integer.parseInt(people_num.getText().toString())==5){
                    Toast.makeText(ActivitySignUpActivity.this,"每单仅限五人", Toast.LENGTH_SHORT).show();
                }else {

                    people_num.setText(Integer.toString(Integer.parseInt(people_num.getText().toString()) +1));
                    act_price.setText(toAllPrice(Integer.parseInt(people_num.getText().toString())));
                }
                break;
            case R.id.act_jian:
                if(Integer.parseInt(people_num.getText().toString())==1){
                    Toast.makeText(ActivitySignUpActivity.this,"最低选择一人", Toast.LENGTH_SHORT).show();
                }else {
                    people_num.setText(Integer.toString(Integer.parseInt(people_num.getText().toString()) -1));
                    act_price.setText(toAllPrice(Integer.parseInt(people_num.getText().toString())));
                }
                break;
            case R.id.act_signup:
                Intent intent = new Intent(ActivitySignUpActivity.this,ActivityDetailsActivity.class);
                startActivity(intent);
                finish();
                Toast.makeText(ActivitySignUpActivity.this,"结算成功", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
